using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using iBeauty.BusinessObjects;

namespace iBeauty.DAL
{
	public class ProductGroupDAO
	{
		public ProductGroupDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<ProductGroup> GetProductGroups()
		{
			try
			{
				List<ProductGroup> lstProductGroups = new List<ProductGroup>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [ID],[CreateDate],[ModifiedDate],[TimeStamp],[GroupCode],[GroupName],[AccountId],[Note] FROM [ProductGroup]",CommandType.Text);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					ProductGroup oProductGroup = new ProductGroup();
					oProductGroup.ID = Convert.ToString(oDbDataReader["ID"]);

					if(oDbDataReader["CreateDate"] != DBNull.Value)
						oProductGroup.CreateDate = Convert.ToDateTime(oDbDataReader["CreateDate"]);

					if(oDbDataReader["ModifiedDate"] != DBNull.Value)
						oProductGroup.ModifiedDate = Convert.ToDateTime(oDbDataReader["ModifiedDate"]);

					if(oDbDataReader["TimeStamp"] != DBNull.Value)
						oProductGroup.TimeStamp = Convert.ToDateTime(oDbDataReader["TimeStamp"]);

					if(oDbDataReader["GroupCode"] != DBNull.Value)
						oProductGroup.GroupCode = Convert.ToString(oDbDataReader["GroupCode"]);

					if(oDbDataReader["GroupName"] != DBNull.Value)
						oProductGroup.GroupName = Convert.ToString(oDbDataReader["GroupName"]);

					if(oDbDataReader["AccountId"] != DBNull.Value)
						oProductGroup.AccountId = Convert.ToInt32(oDbDataReader["AccountId"]);

					if(oDbDataReader["Note"] != DBNull.Value)
						oProductGroup.Note = Convert.ToString(oDbDataReader["Note"]);
					lstProductGroups.Add(oProductGroup);
				}
				oDbDataReader.Close();
				return lstProductGroups;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public ProductGroup GetProductGroup(string ID)
		{
			try
			{
				ProductGroup oProductGroup = new ProductGroup();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [ID],[CreateDate],[ModifiedDate],[TimeStamp],[GroupCode],[GroupName],[AccountId],[Note] FROM [ProductGroup] WHERE [ID]=@ID",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,ID));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oProductGroup.ID = Convert.ToString(oDbDataReader["ID"]);

					if(oDbDataReader["CreateDate"] != DBNull.Value)
						oProductGroup.CreateDate = Convert.ToDateTime(oDbDataReader["CreateDate"]);

					if(oDbDataReader["ModifiedDate"] != DBNull.Value)
						oProductGroup.ModifiedDate = Convert.ToDateTime(oDbDataReader["ModifiedDate"]);

					if(oDbDataReader["TimeStamp"] != DBNull.Value)
						oProductGroup.TimeStamp = Convert.ToDateTime(oDbDataReader["TimeStamp"]);

					if(oDbDataReader["GroupCode"] != DBNull.Value)
						oProductGroup.GroupCode = Convert.ToString(oDbDataReader["GroupCode"]);

					if(oDbDataReader["GroupName"] != DBNull.Value)
						oProductGroup.GroupName = Convert.ToString(oDbDataReader["GroupName"]);

					if(oDbDataReader["AccountId"] != DBNull.Value)
						oProductGroup.AccountId = Convert.ToInt32(oDbDataReader["AccountId"]);

					if(oDbDataReader["Note"] != DBNull.Value)
						oProductGroup.Note = Convert.ToString(oDbDataReader["Note"]);
				}
				oDbDataReader.Close();
				return oProductGroup;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddProductGroup(string ID,DateTime?CreateDate,DateTime?ModifiedDate,DateTime?TimeStamp,string GroupCode,string GroupName,int?AccountId,string Note)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERT INTO [ProductGroup]([CreateDate],[ModifiedDate],[TimeStamp],[GroupCode],[GroupName],[AccountId],[Note])VALUES(@CreateDate,@ModifiedDate,@TimeStamp,@GroupCode,@GroupName,@AccountId,@Note)",CommandType.Text);
				if (CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (GroupCode!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupCode",DbType.String,GroupCode));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupCode",DbType.String,DBNull.Value));
				if (GroupName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupName",DbType.String,GroupName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupName",DbType.String,DBNull.Value));
				if (AccountId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,AccountId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,DBNull.Value));
				if (Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));

				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddProductGroup(ProductGroup oProductGroup)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERT INTO [ProductGroup]([CreateDate],[ModifiedDate],[TimeStamp],[GroupCode],[GroupName],[AccountId],[Note])VALUES(@CreateDate,@ModifiedDate,@TimeStamp,@GroupCode,@GroupName,@AccountId,@Note)",CommandType.Text);
				if (oProductGroup.CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,oProductGroup.CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (oProductGroup.ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,oProductGroup.ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (oProductGroup.TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,oProductGroup.TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (oProductGroup.GroupCode!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupCode",DbType.String,oProductGroup.GroupCode));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupCode",DbType.String,DBNull.Value));
				if (oProductGroup.GroupName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupName",DbType.String,oProductGroup.GroupName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupName",DbType.String,DBNull.Value));
				if (oProductGroup.AccountId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,oProductGroup.AccountId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,DBNull.Value));
				if (oProductGroup.Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,oProductGroup.Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));

				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateProductGroup(string ID,DateTime?CreateDate,DateTime?ModifiedDate,DateTime?TimeStamp,string GroupCode,string GroupName,int?AccountId,string Note)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [ProductGroup] SET [CreateDate]=@CreateDate,[ModifiedDate]=@ModifiedDate,[TimeStamp]=@TimeStamp,[GroupCode]=@GroupCode,[GroupName]=@GroupName,[AccountId]=@AccountId,[Note]=@Note WHERE [ID]=@ID",CommandType.Text);
				if (CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (GroupCode!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupCode",DbType.String,GroupCode));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupCode",DbType.String,DBNull.Value));
				if (GroupName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupName",DbType.String,GroupName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupName",DbType.String,DBNull.Value));
				if (AccountId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,AccountId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,DBNull.Value));
				if (Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateProductGroup(ProductGroup oProductGroup)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [ProductGroup] SET [CreateDate]=@CreateDate,[ModifiedDate]=@ModifiedDate,[TimeStamp]=@TimeStamp,[GroupCode]=@GroupCode,[GroupName]=@GroupName,[AccountId]=@AccountId,[Note]=@Note WHERE [ID]=@ID",CommandType.Text);
				if (oProductGroup.CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,oProductGroup.CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (oProductGroup.ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,oProductGroup.ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (oProductGroup.TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,oProductGroup.TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (oProductGroup.GroupCode!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupCode",DbType.String,oProductGroup.GroupCode));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupCode",DbType.String,DBNull.Value));
				if (oProductGroup.GroupName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupName",DbType.String,oProductGroup.GroupName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@GroupName",DbType.String,DBNull.Value));
				if (oProductGroup.AccountId.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,oProductGroup.AccountId));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@AccountId",DbType.Int32,DBNull.Value));
				if (oProductGroup.Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,oProductGroup.Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,oProductGroup.ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveProductGroup(ProductGroup oProductGroup)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [ProductGroup] WHERE [ID]=@ID",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,oProductGroup.ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveProductGroup(string ID)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [ProductGroup] WHERE [ID]=@ID",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
